import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Home from '@/views/home.vue'
import 'nprogress/nprogress.css'
import { check } from '@/api/user'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/dashboard',
  },
  {
    path: '/',
    name: 'Home',
    component: Home,
    children: [
      {
        path: '/file/upload',
        component: () => import('@/views/user/components/FileUpload.vue'),
      },
      {
        path: '/dashboard',
        meta: {
          title: '系统首页',
          noAuth: true,
          keepAlive: true,
        },
        component: () => import('@/views/dashboard/Dashboard.vue'),
      },
      {
        path: '/system-user',
        name: 'system-user',
        meta: {
          title: '用户管理',
          noAuth: true,
          keepAlive: true,
        },
        component: () => import('@/views/system/SystemUser.vue'),
      },
      {
        path: '/system-role',
        name: 'system-role',
        meta: {
          title: '角色管理',
          noAuth: true,
          keepAlive: true,
        },
        component: () => import('@/views/system/SystemRole.vue'),
      },
      {
        path: '/system-menu',
        name: 'system-menu',
        meta: {
          title: '菜单管理',
          noAuth: true,
          keepAlive: true,
        },
        component: () => import('@/views/system/SystemMenu.vue'),
      },
      {
        path: '/blog-list',
        name: 'blog-list',
        meta: {
          title: '列表',
          keepAlive: true,
        },
        component: () => import('@/views/blog/blog-list/index.vue'),
      },
      {
        path: '/blog-tag',
        name: 'blog-tag',
        meta: {
          title: '标签',
          keepAlive: true,
        },
        component: () => import('@/views/blog/blog-tag/index.vue'),
      },
      {
        path: '/blog-category',
        name: 'blog-category',
        meta: {
          title: '分类',
          keepAlive: true,
        },
        component: () => import('@/views/blog/blog-category/index.vue'),
      },
      {
        path: '/blog-comment',
        name: 'blog-comment',
        meta: {
          title: '评论',
          keepAlive: true,
        },
        component: () => import('@/views/blog/blog-comment/index.vue'),
      },

      {
        path: '/user-center',
        meta: {
          title: '个人中心',
          keepAlive: true,
        },
        component: () => import('@/views/user/UserCenter.vue'),
      },
      {
        path: '/weibo-list',
        name: 'weibo-list',
        meta: {
          title: '列表',
          keepAlive: true,
        },
        component: () => import('@/views/weibo/WeiboList.vue'),
      },
      {
        path: '/timeline-list',
        name: 'timeline-list',
        meta: {
          title: '时间线',
          keepAlive: true,
        },
        component: () => import('@/views/timeline/TimelineList.vue'),
      },
    ],
  },
  {
    path: '/blog-view',
    name: 'blog-view',
    meta: {
      title: '预览',
    },
    component: () => import('@/views/blog/blog-view/index.vue'),
  },
  {
    path: '/login',
    meta: {
      title: '登录',
      noAuth: true,
    },
    component: () => import('@/views/login/Login.vue'),
  },
  {
    path: '/change-pwd',
    name: 'change-pwd',
    meta: {
      title: '修改密码',
      noAuth: true,
    },
    component: () => import('@/views/user/ResetPwd.vue'),
  },
  {
    path: '/403',
    meta: {
      title: '没有权限',
      noAuth: true,
    },
    component: () => import('@/views/error/403.vue'),
  },
  {
    path: '/404',
    meta: {
      title: '找不到页面',
      noAuth: true,
    },
    component: () => import('@/views/error/404.vue'),
  },
  {
    path: '/test',
    meta: {
      title: 'AI Chat测试',
      keepAlive: false,
    },
    component: () => import('@/views/test/Test.vue'),
  },
  { path: '/:path(.*)', redirect: '/404' },
]

const router = createRouter({
  history: createWebHistory(),
  routes,
})

let isChecked = false // 定义全局变量，默认为false

router.beforeEach(async (_to, _from, next) => {
  if (!isChecked && _to.path !== '/login') {
    try {
      await check() // 调用check接口
      isChecked = true // 接口调用成功后将全局变量设置为true
      next() // 继续路由
    } catch (error) {
      console.error('检查失败:', error)
      next('/login') // 如果检查失败，重定向到登录页面
    }
  } else {
    next() // 如果已检查，继续路由
  }
})

export default router
